<?php
namespace app\admin\model;
class User extends CommonModel{
    private $table_name = 'user';
    public function __construct()
    {
        parent::__construct();
    }

    /**初始化参数
     * @param $args [client_id,token,enc_string,nickname]
     * @return array
     */
    private function default_param($args)
    {
        if(empty($args)) return;
        $find = [
            'client_id'       => "",
            "token"         => "",
            "enc_string"    => "",
            "nickname"      => "匿名",
            "kf_username"  => "",
            "kf_password"   => "",
        ];
        $args = array_merge($find,$args);
        return $args;
    }


    /**
     * 新增一个用户
     * @param array $param ['kf_id']可传选项
     * @return client_id,加密后token
     */
    public function add_user($param = [])
    {
        $param = $this->default_param($param);
        $args = $this->getSecrect();
        //插入基本信息
        $client_id =  db($this->table_name)->insertGetId([
            'client_secrect' => $args['client_secrect'],
            'enc_string'     => $args['enc_string'],
            'nickname'       => $param['nickname'],
            'kf_id'         => isset($param['kf_id']) ? $param['kf_id'] : ""
        ]);
        //更新加密的客户端id
        db($this->table_name)->where(['client_id'=>$client_id])->update(['enc_client_id'=>$this->EncUserid($client_id)]);

        return ['client_id'=>$client_id,'token'=>$args['token'],'nickname'=>'匿名','is_kf'=>0,'user_name'=>'匿名'];
    }

    /**生成一个加密token 并返回加密的信息细节
     * @param array $args [client_secrect] 如果传入client_secrect键，则表明只生成enc_string，而client_secrect则根据传入参数来定
     * @return array
     */
    private function getSecrect($args = [])
    {
        $enc_string = randString(15);
        $client_secrect = isset($args['client_secrect']) ? $args['client_secrect'] : randString(20);
        return [
            'enc_string' => $enc_string,
            'client_secrect' => $client_secrect,
            'token'     => $this->getToken(['enc_string'=>$enc_string,'client_secrect'=>$client_secrect])
        ];
    }

    /**加密一个token
     * @param $args [enc_string,client_secrect] 根据这两个键值加密
     * @return string
     */
    public function getToken($args)
    {
        return md5($args['enc_string'].$args['client_secrect']);
    }

    /** 登录成功后，重设enc_string
     * @param $args client_id,client_secrect
     * @return array client_id,token
     * @return array client_id,token
     */
    public function resetEncString($args)
    {
        $secrect = $this->getSecrect(['client_secrect'=>$args['client_secrect']]);
        $this->updateEncString(['client_id'=>$args['client_id'],'enc_string'=>$secrect['enc_string']]);

        return $secrect['token'];
    }


    /**更新一个用户的随机加密窜
     * @param $args [id,enc_string]
     */
    public function updateEncString($args)
    {
        db($this->table_name)->where(['client_id'=>$args['client_id']])->update(['enc_string'=>$args['enc_string']]);
    }

}